<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Batch_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	function insert_batch($data){
		$this->db->insert('BATCH',$data);
	}
	
	//list flexy
	function get_data_batch($id){
		//Select table name
		$table_name = "BATCH";
		
		//Build contents query
		$this->db->select('*')->from($table_name);
		$this->db->where("ID_LISENSI", $id);
		$this->db->where("STATUS > 0 ");
		$this->CI->flexigrid->build_query();
		
		//Get contents
		$return['records'] = $this->db->get();
		
		//Build count query
		$this->db->select('*')->from($table_name);
                $this->db->where("ID_LISENSI", $id);
		$this->db->where("STATUS > 0 ");
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();
	
		//Return all
		return $return;
	}
        
	
	
	function get_data_all_lisensi_batch(){
		//Select table name
		$table_name = "BATCH";
		
		//Build contents query
		$this->db->select('BATCH.*, LISENSI.NAMA_LISENSI')->from($table_name);
		$this->db->join('LISENSI', 'BATCH.ID_LISENSI = LISENSI.ID_LISENSI', 'left');
		$this->db->where("BATCH.STATUS > 0 ");
		$this->CI->flexigrid->build_query();
		
		//Get contents
		$return['records'] = $this->db->get();
		
		//Build count query
		$this->db->select('BATCH.*, LISENSI.NAMA_LISENSI')->from($table_name);
		$this->db->join('LISENSI', 'BATCH.ID_LISENSI = LISENSI.ID_LISENSI', 'left');
		$this->db->where("BATCH.STATUS > 0 ");
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();
	
		//Return all
		return $return;
	}
	
	//get data for dropdown
	function get_batch($id){
		
		$this->db->select('
		*,
		DAY(TANGGAL_ENTRY) as TANGGAL_ENTRY,
		MONTH(TANGGAL_ENTRY) as BULAN_ENTRY,
		YEAR(TANGGAL_ENTRY) as TAHUN_ENTRY,
		DAY(TANGGAL_SELESAI) as TANGGAL_SELESAI,
		MONTH(TANGGAL_SELESAI) as BULAN_SELESAI,
		YEAR(TANGGAL_SELESAI) as TAHUN_SELESAI
		');
		$this->db->from('BATCH');
		$this->db->where("STATUS = 2 ");
		$this->db->where("ID_LISENSI", $id);
		$this->db->order_by('NAMA_BATCH');
		$query = $this->db->get();
		return $query;
	}
	
	
	//get data for dropdown
	function get_all_batch(){
		$this->db->select('
		*,
		DAY(TANGGAL_ENTRY) as TANGGAL_ENTRY,
		MONTH(TANGGAL_ENTRY) as BULAN_ENTRY,
		YEAR(TANGGAL_ENTRY) as TAHUN_ENTRY,
		DAY(TANGGAL_SELESAI) as TANGGAL_SELESAI,
		MONTH(TANGGAL_SELESAI) as BULAN_SELESAI,
		YEAR(TANGGAL_SELESAI) as TAHUN_SELESAI
		');
		$this->db->from('BATCH');
		$this->db->where("STATUS = 2");
		$this->db->order_by('NAMA_BATCH');
		$query = $this->db->get();
		return $query;
	}
	
	
	function selectone($id){
		$this->db->select("*");
		$this->db->from('BATCH');
		$this->db->where('ID_BATCH',$id);
		$query = $this->db->get();
		return $query;
	}
        
	function cek_tanggal($tanggal_mulai, $tanggal_selesai){
		$this->db->select("*");
		$this->db->from('BATCH');
		
		$this->db->where('TANGGAL_SELESAI <=',$tanggal_selesai);
                $this->db->where('TANGGAL_ENTRY >=',$tanggal_mulai);
		$query = $this->db->get();
		return $query;
	}
	
	function update_batch($id, $data){
		$this->db->where('ID_BATCH',$id);
		$this->db->update('BATCH',$data);
	}
	
	function delete($id){
	$data = array(
			'STATUS' 			=> 0
		);
		$this->db->where('ID_BATCH',$id);
		$this->db->update('BATCH',$data);
	}
	
	function go_online($id){
	$data = array(
			'STATUS' 			=> 2
		);
		$this->db->where('ID_BATCH',$id);
		$this->db->update('BATCH',$data);
	}
	
	function go_offline($id){
	$data = array(
			'STATUS' 			=> 3
		);
		$this->db->where('ID_BATCH',$id);
		$this->db->update('BATCH',$data);
	}
	
	function get_user(){
		$this->db->select("*");
		$this->db->from('USERS');
		$this->db->where("STATUS > 0 ");
		$query = $this->db->get();
		return $query;
	}
	
	function get_menteri(){
		$this->db->select('*');
		$this->db->from('USERS');
		$this->db->where('STATUS = 2');
		$query = $this->db->get();
		return $query;
	}
	
}

?>